Check USERNAME for a users's name with `cargo new`
authorAlex Crichton <alex@alexcrichton.com>
Wed, 29 Oct 2014 23:27:16 +0000 (16:27 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 30 Oct 2014 00:29:19 +0000 (17:29 -0700)
Closes #740

src/cargo/ops/cargo_new.rs
tests/test_cargo_new.rs

index d5c0d18ef895fd8958336eb5eddb353c1f321f2e..1fc5ab7d0cdce9e5aed6a1c734608c1e52521388 100644 (file)
@@ -111,11 +111,15 @@ fn discover_author() -> CargoResult<(String, Option<String>)> {
     let git_config = git_config.as_ref();
     let name = git_config.and_then(|g| g.get_str("user.name").ok())
                          .map(|s| s.to_string())
-                         .or_else(|| os::getenv("USER"));
+                         .or_else(|| os::getenv("USER"))      // unix
+                         .or_else(|| os::getenv("USERNAME")); // windows
     let name = match name {
         Some(name) => name,
-        None => return Err(human("could not determine the current user, \
-                                  please set $USER"))
+        None => {
+            let username_var = if cfg!(windows) {"USERNAME"} else {"USER"};
+            return Err(human(format!("could not determine the current \
+                                      user, please set ${}", username_var)))
+        }
     };
     let email = git_config.and_then(|g| g.get_str("user.email").ok());
 
index 7539a0475c0483ac148d46ceecb38ea01e770815..79694d56e50ecad304981290516fa8c2c538d774 100644 (file)
@@ -120,6 +120,21 @@ test!(finds_author_user {
     assert!(toml.as_slice().contains(r#"authors = ["foo"]"#));
 })
 
+test!(finds_author_username {
+    // Use a temp dir to make sure we don't pick up .cargo/config somewhere in
+    // the hierarchy
+    let td = TempDir::new("cargo").unwrap();
+    assert_that(cargo_process("new").arg("foo")
+                                    .env("USER", None::<&str>)
+                                    .env("USERNAME", Some("foo"))
+                                    .cwd(td.path().clone()),
+                execs().with_status(0));
+
+    let toml = td.path().join("foo/Cargo.toml");
+    let toml = File::open(&toml).read_to_string().assert();
+    assert!(toml.as_slice().contains(r#"authors = ["foo"]"#));
+})
+
 test!(finds_author_git {
     my_process("git").args(["config", "--global", "user.name", "bar"])
                      .exec().assert();